<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class lostpass extends CI_Controller 
{

	function __construct()
	{
		parent::__construct();
		$this->load->helper('mail');
	}
	
	function index()
	{
		$data['title'] = 'Quên mật khẩu' ;
		$data['page'] = 'lostpass' ;
		$this->load->view('html/skin_raovat', $data);

	}
	
	function action()
	{
		if($_POST['Submit']=='Get New Password')
		{
			$this->form_validation->set_rules('email','Chọn sản phẩm','trim|required');  
			if($this->form_validation->run() == FALSE)
			{
				$this->pre_message = validation_errors();
			}
			else
			{
				if($this->lostpass->reset_password())
				{
					sendmail($name,$from,$to,$subject,$message) ;
					$this->session->set_flashdata('message','Send mail successful. Please check your email and login again.');
					redirect('lostpass/success');
				}
				else
				{
					$this->pre_message = 'Your email is not found.';
					redirect('lostpass');
				}
			} 
		}
		redirect('login');
	}
	
	function success()
	{
		
	}
	
	function _make_captcha()
	{
		$this ->load -> plugin('captcha');
		$vals = array(
			'img_path' => './captcha/',
			'img_url' => base_url().'captcha/', 
			'img_width' => 60, // width
			'img_height' => 22, // height
			'font_path'     => './system/fonts/chaudoc.ttf',
			'expiration' => 600 , 
		); 
		// Create captcha
		$cap = create_captcha($vals); 
		// Write to DB
		if ($cap) 
		{
			$data = array(
				'captcha_id' => '',
				'captcha_time' => $cap['time'],
				'ip_address' => $this -> input -> ip_address(),
				'word' => $cap['word'] , 
			);
			$query = $this->db->insert_string('fm_captcha', $data );
			$this->db->query($query );
		}
		else 
		{
			return "Mã an toàn không hoạt động" ;
		}
		return $cap['image'] ;
	} 
	
	// CHECK CAPTCHA
	function _check_mabaove($mabaove)
	{ 
		// Delete old data ( 2hours)
		$expiration = time()-600 ;
		$sql = " DELETE FROM fm_captcha WHERE captcha_time < ? ";
		$binds = array($expiration);
		$query = $this->db->query($sql, $binds);
		
		//checking input
		$sql = "SELECT COUNT(*) AS count FROM fm_captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
		$binds = array($mabaove, $this->input->ip_address(), $expiration);
		$query = $this->db->query($sql, $binds);
		$row = $query->row();
		
		if ( $row -> count > 0 ){
			return true;
		}else{
			return false;
		}
	} 
}

?>